

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Private
 */
@WebServlet("/Private")
public class Private extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Private() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//Just redirect to private JSP
		HttpSession session = request.getSession();
		PrintWriter output = response.getWriter();
		checkSession(request, response, session);

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		PrintWriter output = response.getWriter();
		checkSession(request, response, session);
	}

	private void checkSession(HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		String login = request.getParameter("login");
		String password=request.getParameter("password");
		if(login.equals("Casel") && password.equals("Casel")){
			Pattern pattern= Pattern.compile("^[a-z0-9A-Z]{5}");
		java.util.regex.Matcher loginChecker = pattern.matcher(login);
			System.out.print("Login Correct");
			session.setAttribute("Login",login);
			this.getServletContext().getRequestDispatcher("/Private").forward(request, response);
		}else{
			System.out.print("Login Incorrect");
			this.getServletContext().getRequestDispatcher("/SingIn").forward(request, response);
		}
	}

}
